<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jinzhao
 * Date: 16-8-18
 * Time: 下午1:17
 */
class Address_model extends CI_Model{
    private $error = '';
    
    
    public function create($addr_info = array())
    {
        $insert = array(
            'name' => $addr_info['name'],
            'ip' => $addr_info['ip'],
            'port' => $addr_info['port'],
            'uid' => $this->session->userdata('uid')
        );
        $result = $this->db->insert('address', $insert);
        if (!$result || $this->db->affected_rows() == 0){
            $this->error = '添加失败';
            return false;
        }
        $this->init_count();
        return true;
    }
    
    public function get($offset= '', $value = 0)
    {
        $user = $this->session->userdata('uid');

        $result = $this->db->select('id,name,ip,port')->
        from('address')->
        where(array('uid' => $user))->
        limit($value, $offset)->
        get()->result_array();
        if (!$result){
            $this->error = '获取失败';
            return false;
        }
        return $result;
    }
    
    public function init_count()
    {
        $result = $this->db->select('count(id) as numbers')->
        from('address')->
        where(array('uid' => $this->session->userdata('uid')))->
        get()->
        result_array();
        if ($result && count($result) > 0){
            $this->session->set_userdata('address_numbers', $result[0]['numbers']);
        }
    }
    
    public function delete($id = '')
    {
        if ($id == ''){
            return false;
        }

        $result = $this->db->delete('address', array('id' => $id));
        if (!$result || $this->db->affected_rows() == 0){
            return false;
        }
        $this->init_count();
        return true;
    }
    
    
    public function update()
    {
        $id = $this->input->post('id');
        $name = $this->input->post('name');
        $ip = $this->input->post('ip');
        $port = $this->input->post('port');
        
        $user = $this->session->userdata('uid');
        
        $data = array(
            'name' => $name,
            'ip' => $ip,
            'port' => $port
        );
        
        $where = array(
            'id' => $id,
            'uid' => $user
        );
        
        $result = $this->db->update('address', $data, $where);
        if (!$result || $this->db->affected_rows() == 0){
            $this->error = '无法更新';
            return false;
        }
        return true;
    }
    
    public function get_by_id($id = '')
    {
        $user = $this->session->userdata('uid');

        $result = $this->db->select('*')->
        from('address')->
        where(array('uid' => $user, 'id' => $id))->
        get()->result_array();
        if (!$result){
            $this->error = '获取失败';
            return false;
        }
        return $result;
    }
    
    
    public function display_errors()
    {
        return $this->error;
    }
    
}